#include "Mutex.hpp"

Mutex::Mutex(void)
{
	mutex = PTHREAD_MUTEX_INITIALIZER;
	State = eUnlock;
}

Mutex::~Mutex(void)
{
	//assure its unlock before total delete
	UnLock();
	//destroy mutex
	pthread_mutex_destroy(&mutex);
	free(mutex);
}

bool Mutex::Lock(void)
{
	if(pthread_mutex_lock(&mutex) == 0)
	{
		State = eLock;
		return true;
	}
	else 
		return false;
}

bool Mutex::UnLock(void)
{
	if(pthread_mutex_unlock(&mutex) == 0)
	{
		State = eUnlock;
		return true;
	}
	else 
		return false;
}

bool Mutex::TryLock(void)
{
	if(pthread_mutex_trylock(&mutex) == 0)
	{
		State = eLock;
		return true;
	}
	else
		return false;
}


eMutexState Mutex::GetState(void)
{
	return State;
}


/*
	private:
		eMutexState State;
		pthread_mutex_t mutex;


# pthread_mutex_destroy [pthread_mutex_init] - operations on mutexes
# pthread_mutex_init - operations on mutexes
# pthread_mutex_lock [pthread_mutex_init] - operations on mutexes
# pthread_mutex_trylock [pthread_mutex_init] - operations on mutexes
# pthread_mutex_unlock [pthread_mutex_init] - operations on mutexes 
*/

